package com.henke.convertruck;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class VolumeActivity extends Activity {
	public int ItemSelectedFrom;
	public int ItemSelectedTo;
	
	public String stringFrom;
	public String stringTo;
	
	private EditText textInfo;
	private TextView textHistory;

	public void setItemSelectedFrom(int itemSelectedFrom) {
		ItemSelectedFrom = itemSelectedFrom;
	}

	public void setItemSelectedTo(int itemSelectedTo) {
		ItemSelectedTo = itemSelectedTo;
	}

	public void setStringFrom(String stringFrom) {
		this.stringFrom = stringFrom;
	}

	public void setStringTo(String stringTo) {
		this.stringTo = stringTo;
	}

	public void setTextInfo(EditText textInfo) {
		this.textInfo = textInfo;
	}

	public void setTextHistory(TextView textHistory) {
		this.textHistory = textHistory;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_volume);

		/*-------------------------------------*/
		textInfo = (EditText) findViewById(R.id.editText1);
		textHistory = (TextView) findViewById(R.id.textViewHistory);
		
		
		Spinner spinnerFrom = (Spinner) findViewById(R.id.spinnerFrom);
		Spinner spinnerTo	= (Spinner) findViewById(R.id.spinnerTo);
		
		// Create an ArrayAdapter using the string array and a default spinner layout
		final ArrayAdapter<CharSequence> adapterFrom = ArrayAdapter.createFromResource(this,
		        R.array.VolumeListFrom, android.R.layout.simple_spinner_item);
		final ArrayAdapter<CharSequence> adapterTo = ArrayAdapter.createFromResource(this,
		        R.array.VolumeListTo, android.R.layout.simple_spinner_item);
		
		// Specify the layout to use when the list of choices appears
		adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		// Apply the adapter to the spinner
		spinnerFrom.setAdapter(adapterFrom);
		spinnerTo.setAdapter(adapterTo);
		
		// create a Title
		spinnerFrom.setPrompt("From");
		spinnerTo.setPrompt("To");
		
		spinnerFrom.setSelection(0);
		spinnerTo.setSelection(0);
		
		// set click handler for spinnerFrom
		spinnerFrom.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
				//Toast.makeText(getBaseContext(), "Convert from " + adapterFrom.getItem(position), Toast.LENGTH_SHORT).show();
				setItemSelectedFrom(position);
				setStringFrom((String) adapterFrom.getItem(position));
			}
			@Override
		    public void onNothingSelected(AdapterView<?> arg0) {
		    }
		});
		
		// set click handler for spinnerTo
		spinnerTo.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
				//Toast.makeText(getBaseContext(), "To " + adapterTo.getItem(position), Toast.LENGTH_SHORT).show();
				setItemSelectedTo(position); 
				setStringTo((String) adapterTo.getItem(position));
			}
			@Override
		    public void onNothingSelected(AdapterView<?> arg0) {
		    }
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.volume, menu);
		return true;
	}
	
	/*---------------------------------------------------------------------------------*/

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		TextView tvInfo = (TextView) findViewById(R.id.textViewHistory);

		// Operations for the selected menu item
		switch (item.getItemId()) {
		case R.id.menu_length:
			Toast.makeText(this, "You have chosen a Length Converter", Toast.LENGTH_LONG).show();
			Intent mainIntent = new Intent(VolumeActivity.this, MainActivity.class);
			startActivity(mainIntent);
			return true;
		case R.id.menu_volume:
			Toast.makeText(this, "You have chosen a Volume Converter", Toast.LENGTH_LONG).show();
			return true;
		case R.id.menu_temperature:
			Toast.makeText(this, "You have chosen a Temperature Converter", Toast.LENGTH_LONG).show();
			Intent tempIntent = new Intent(VolumeActivity.this, TemperatureActivity.class);
			startActivity(tempIntent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	

	/***********************************************************************************/
	

}
